//
//  TestViewController.m
//  AFNetWorking再封装
//
//  Created by 刘雪龙 on 2020/3/25.
//  Copyright © 2020 刘雪龙. All rights reserved.
//

#import "TestViewController.h"
#import "JsonDataModel.h"
#import "CustomCollectionViewCell.h"
@interface TestViewController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>

@property (nonatomic, assign) NSInteger  current_page;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) JsonDataModel *dataModel;
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self instancetypeProperty];
    [self requestPostWithData];
    [self instancetypeCollectionView];
}

#pragma mark -- 构造属性
-(void)instancetypeProperty{
    self.view.backgroundColor=[UIColor whiteColor];
    self.current_page=1;
    self.automaticallyAdjustsScrollViewInsets = NO;

    char *str1="sdafadsf";
    
    NSString *str2=@"45yu";
    
    str2=[self reversalString:str2];
    
    
    SLog(@"4>>>>>>%@",str2);
    
}

- (NSString *)reversalString:(NSString *)originString{
    NSString *resultStr = @"";
    for (NSInteger i = originString.length -1; i >= 0; i--) {
      NSString *indexStr = [originString substringWithRange:NSMakeRange(i, 1)];
      resultStr = [resultStr stringByAppendingString:indexStr];
    }
  return resultStr;
}



void char_reverse (char *cha) {

    // 定义头部指针
    char *begin = cha;
    // 定义尾部指针
    char *end = cha + strlen(cha) -1;
    
    
    while (begin < end) {
        
        char temp = *begin;
        *(begin++) = *end;
        *(end--) = temp;
    }
}


#pragma mark --参数构建--
- (NSMutableDictionary *)instancetypeBodyParameters{
    NSString *page_string=[NSString stringWithFormat:@"%ld",self.current_page];
    NSDictionary *value_dic=@{@"userToken":@"8ed7ddfde5ca37849b7c43591957d7fb73adc2a1",
                              @"pageNumber": page_string};
    NSString *value_string=[self dictionaryToJson:value_dic];
    NSMutableDictionary *bodyParameters = [NSMutableDictionary dictionary];
    [bodyParameters setObject:value_string forKey:@"requestData"];
    return bodyParameters;
}

//字典转化字符串
-(NSString*)dictionaryToJson:(NSDictionary *)dic{
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}


#pragma mark --数据请求--
-(void)headerRefreshData{
     self.current_page=1;
    [self.collectionView.mj_header beginRefreshing];
    [self requestPostWithData];
}


-(void)loadMoreDta{
    self.current_page+=1;
    [self.collectionView.mj_footer beginRefreshing];
    [self requestPostWithData];
}


-(void)requestPostWithData{
    NSMutableDictionary *bodyParameters = [self instancetypeBodyParameters];
    [NetWorkManager requestWithType:HttpRequestTypePost withUrlString:API withParaments:bodyParameters withSuccessBlock:^(NSDictionary *object) {
        [self dealWithListData:object];
    } withFailureBlock:^(NSError *error) {
        [self endRefreshing];
    } progress:^(float progress) {
        [self endRefreshing];
    }];
}


//数据处理
-(void)dealWithListData:(id)responseObject{
    if ([responseObject[@"success"] integerValue]==1){
        SLog(@"%@",responseObject);
        self.dataModel=[[JsonDataModel alloc]initWithDictionary:responseObject error:nil];
        if (self.current_page==1) {
            self.dataArray=[NSMutableArray arrayWithArray:self.dataModel.data];
        }else{
            NSArray *newArray=[NSArray arrayWithArray:self.dataModel.data];
            [self.dataArray addObjectsFromArray:newArray];
        }
        
        [self.collectionView reloadData];
        [self endRefreshing];
    }
}

//结束数据刷新
-(void)endRefreshing{
    if (self.dataModel.data.count==0) {
        [self.collectionView.mj_footer endRefreshingWithNoMoreData];
    }else{
        [self.collectionView.mj_footer endRefreshing];
    }
    [self.collectionView.mj_header endRefreshing];
}


#pragma mark --布局--

-(void)instancetypeCollectionView{
    
    if (!_collectionView) {
        /*属性设置*/
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.minimumInteritemSpacing = 5;//列间距
        layout.minimumLineSpacing = 0;//行间距
        layout.itemSize = CGSizeMake(item_width, item_height);//设置item大小
        layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);//设置上左下右边距

        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, kNavSystemHeight, KScreenWidth,KScreenHeight-kNavSystemHeight) collectionViewLayout:layout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.showsVerticalScrollIndicator =NO;
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.backgroundColor =[UIColor whiteColor];
        [self.view addSubview:_collectionView];

        //下拉刷新
       MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefreshData)];
        _collectionView.mj_header = header;
        
       //上拉加载
       MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreDta)];
        _collectionView.mj_footer = footer;
        [footer setTitle:@"没有更多数据啦！" forState:MJRefreshStateNoMoreData];
        
        //注册cell
        [_collectionView registerNib:[UINib nibWithNibName:@"CustomCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CustomCollectionViewCell"];
    }
}

#pragma mark --UICollectionViewDataSource--

//item数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}


//每个item
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCollectionViewCell" forIndexPath:indexPath];
    JsonDataItemModel *model=self.dataArray[indexPath.row];
    [cell setCellContentWithModel:model];
    return cell;
}



@end
